package com.hdoit.kylin.web.money.controllers;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.hdoit.kylin.logical.money.model.Asset;
import com.hdoit.kylin.logical.money.service.IAssetService;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.money.AssetSearch;
import com.hdoit.kylin.web.common.CommonWebUtils;
/**
*@author liyaochong
*@email
*@createtime 2017年07月27日 14:59:44
*@comment
*/
@Controller
@RequestMapping(value="/admin/money/asset/")
public class AssetController extends BaseController{
	@Override
	public String getRoutePath(){
		return "areas/admin/money/asset/";
	}
	
	private IAssetService assetService;
	public IAssetService getAssetService(){
		return assetService;
	}
	@Resource
	public void setAssetService(IAssetService assetService) {
		this.assetService=assetService;
	}
	
	private IStudentService studentService;
	public IStudentService getStudentService(){
		return studentService;
	}
	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService=studentService;
	}
	
	private ITeacherService teacherService;
	public ITeacherService getTeacherService(){
		return teacherService;
	}
	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService=teacherService;
	}
	
	private final String indexViewName="index";
	private final String outputViewName="output";
	private final String detailedViewName="assetDetailed";
	private final String saveViewName="save";
	private final String deleteViewName= "delete";
	
	@RequestMapping(value=indexViewName,method= {RequestMethod.GET,RequestMethod.POST})
	public String index(Model model,PagerModel pagerModel,AssetSearch search,boolean ajaxPost){
		if(CommonWebUtils.isTeacher(getLoginModel()))
	    	search.setGroupId(teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
		else if(CommonWebUtils.isStudent(getLoginModel()))
    		search.setGroupId(studentService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
		pagerModel.setTotalAmount(assetService.queryCountByGroupId(search));
		List<Asset>assets=assetService.queryListByGroupId(search,pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("assets", assets);
		model.addAttribute("assetSearch", search);
		if(ajaxPost){
			return view(outputViewName);
		}
			return view(indexViewName);
		}
	
	@RequestMapping(value=detailedViewName,method ={RequestMethod.GET})
	public String assetDetailed(Model model,String key){
		Asset asset=new Asset();
		if(!StringUtils.isEmpty(key)){
			asset=assetService.queryModel(key);
		}
		model.addAttribute("asset",asset);
		return view(detailedViewName);
	}
	
	@RequestMapping(value=saveViewName, method ={ RequestMethod.POST})
	public String save(Model model,Asset asset){
		if(StringUtils.isEmpty(asset.getAssetId())){
			asset.setAssetId(CommonUtils.getKeyId());
			if(CommonWebUtils.isTeacher(getLoginModel()))
				asset.setGroupId(teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
			else if(CommonWebUtils.isStudent(getLoginModel()))
				asset.setGroupId(studentService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
			asset.setModelStatus(ModelStatus.add);
		}else{
			asset.setModelStatus(ModelStatus.update);
		}
		ActionResult actionResult;
		if(assetService.applyChange(asset)){
			actionResult=new ActionResult();
		}else{
			actionResult=new ErrorResult();
		}
		model.addAttribute("asset",asset);
		model.addAttribute(ManageContainer.RESULTKEY,actionResult);
		return view(detailedViewName);
	}
	
	@RequestMapping(value=deleteViewName,method= {RequestMethod.POST},produces="text/plain;charset=utf-8")
	public @ResponseBody String delete(String key){
		if(assetService.deleteModel(key)){
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
		}
	}